package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
@Slf4j//日志
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //1.先查套餐基本信息
        //1.1构建条件对象
        LambdaQueryWrapper<Setmeal> setmealwrapper = new LambdaQueryWrapper<>();
        setmealwrapper.like(StrUtil.isNotEmpty(name), Setmeal::getName, name);
        //1.2构建分页对象
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //1.3查询
        page = setmealMapper.selectPage(page, setmealwrapper);
        //2.获取套餐list集合并遍历
        List<Setmeal> setmealList = page.getRecords();
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                //3.根据category_id查询分类对象
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                //将分类名称关联到套餐中
                setmeal.setCategoryName(category.getName());
                //4.根据setmeal的id查询菜品(中间表)列表
                //4.1构建中间表条件对象
                LambdaQueryWrapper<SetmealDish> sdwrapper = new LambdaQueryWrapper<>();
                sdwrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
                //4.2查询套菜菜品集合
                List<SetmealDish> setmealDisheList = setmealDishMapper.selectList(sdwrapper);
                //4.3关联到套餐中
                setmeal.setSetmealDishes(setmealDisheList);


            }
        }//5.返回结果
        return page;
    }
//新增套餐
    @CacheEvict(value = "setmeal",key = "#setmeal.categoryId")
    @Override
    public void save(Setmeal setmeal) {
//1.先保存套餐基本信息
        setmealMapper.insert(setmeal);
        log.info("保存套餐基本信息,id:{},名称:{},价格:{}", setmeal.getId(), setmeal.getName(), setmeal.getPrice());
        //2.取出套餐菜品列表
        List<SetmealDish> dishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (SetmealDish setmealDish : dishList) {
                //关联套餐id
                setmealDish.setSetmealId(setmeal.getId());
                //保存套餐菜品
                setmealDishMapper.insert(setmealDish);
            }
        }

    }
//删除套餐
    @Override
    @CacheEvict(value = "setmeal", allEntries = true)
    public void deleteBatchIds(List<Long> ids) {
        //1.先判断套餐状态
        //1.1构建套餐条件对象
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.in(Setmeal::getId, ids);
        setmealWrapper.eq(Setmeal::getStatus, 1);
        //1.2查询套餐数量
        Integer count = setmealMapper.selectCount(setmealWrapper);
        if (count > 0) {
            throw new CustomException("删除的套餐状态必须为停售*****");
        }
        //2.再删除套餐
        setmealMapper.deleteBatchIds(ids);
        //3.最后删除套餐菜品
        //3.1构建套餐菜品条件对象
        LambdaQueryWrapper<SetmealDish> sdwrapper = new LambdaQueryWrapper<>();
        sdwrapper.in(SetmealDish::getSetmealId, ids);
        //3.2条件删除
        setmealDishMapper.delete(sdwrapper);
    }
//起售停售
@CacheEvict(value = "setmeal", allEntries = true)
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
//条件对象
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        //修改实体
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        //调用mapper
        setmealMapper.update(setmeal, wrapper);
    }
//套餐回显
    @Override
    public Setmeal findById(Long id) {
        //1.先查套餐基本信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //2.查询套餐列表
        //2.1构建套餐查询条件对象
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        //2.2查询列表
        List<SetmealDish> dishList = setmealDishMapper.selectList(wrapper);
        //3.将套餐列表设置到菜品对象中
        setmeal.setSetmealDishes(dishList);
//4.返回菜品对象
        return setmeal;
    }
//套餐修改
    @Override
    @CacheEvict(value = "setmeal", allEntries = true)
    public void update(Setmeal setmeal) {
        //1.先更新套餐的基本信息
        setmealMapper.updateById(setmeal);
        //2.删除套餐原有口味
        //2.1构建口味条件对象
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        //2.2执行mapper删除
        setmealDishMapper.delete(wrapper);
        //3遍历前端提交的口味
        List<SetmealDish> setmealDishesList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishesList)) {
            for (SetmealDish setmealDish : setmealDishesList) {
                //设置套餐id
                setmealDish.setSetmealId(setmeal.getId());
                //调用mapper保存套餐
                setmealDishMapper.insert(setmealDish);
            }
        }
    }
//h5页面套餐展示
    @Cacheable(value = "setmeal",key ="#categoryId" )
    @Override
    public List<Setmeal> setmealList(Long categoryId, Integer status) {
        System.out.println("查询数据库数据");
//1.构建条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId);
        wrapper.eq(Setmeal::getStatus, status);
        //执行查询
        List<Setmeal> setmealList = setmealMapper.selectList(wrapper);
        //返回结果
        return setmealList;
    }
    //查询指定套餐的菜品列表
    @Override
    public List<Dish> getmealDish(Long id) {
        //1.从中间表获取信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        //创建一个集合用于封装返回的菜品集合
        List<Dish> dishList1 = new ArrayList<>();
        //遍历得到菜品列表
        for (SetmealDish setmealDish : setmealDishList) {


            //根据菜品id从菜品表查询菜品性情
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            dish.setCopies(setmealDish.getCopies());//设置份数
            dishList1.add(dish);
        }
        //返回
        return dishList1;

    }
}

